package com.lvmama.service.rhino.searchanalysis.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lvmama.common.result.CodeMessage;
import com.lvmama.common.result.Result;
import com.lvmama.dao.rhino.searchanalysis.FlowSearchDao;
import com.lvmama.pojo.dto.rhino.searchanalysis.*;
import com.lvmama.pojo.entity.rhino.searchanalysis.FlowSearchStation;
import com.lvmama.service.rhino.searchanalysis.FlowSearchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName : FlowSearchServiceImpl
 * @Description : 搜索分析
 * @Author :Administrator
 * @Date : 2018/8/6 16:38
 * @Version V1.0
 **/
@Slf4j
@Service
public class FlowSearchServiceImpl implements FlowSearchService {
    @Autowired
    FlowSearchDao flowSearchDao;

    /**
     * 获取查询时间内各渠道的搜索流量（柱状图）
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchStatistics>> getSearchFlowByChannel(SearchStatisticsQuery queryParam) {
        log.info("getSearchFlowByChannel start. queryParam:{}", ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));

        List<FlowSearchStatistics> flowSearchByChannelList =  null;
        try {
            flowSearchByChannelList =  flowSearchDao.queryFlowByChannel(queryParam);
        }catch(Exception e){
            log.error("queryFlowByChannel error:{}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getSearchFlowByChannel end");
        return Result.success(flowSearchByChannelList);
    }

    /**
     * 获取查询时间内流量趋势（折线图）
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchStatistics>> getSearchFlowByDate(SearchStatisticsQuery queryParam) {
        log.info("getSearchFlowByDate start. queryParam:{}", ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchStatistics> flowSearchByDateList = null;
        try{
            flowSearchByDateList = flowSearchDao.queryFlowByDate(queryParam);
        }catch (Exception e){
            log.error("queryFlowByDate error:{}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getSearchFlowByDate end");
        return Result.success(flowSearchByDateList);
    }

    /**
     * 获取站点信息
     * @return
     */
    @Override
    public Result<List<FlowSearchStation>> getStations() {
        log.info("getStations start");
        List<FlowSearchStation> stationsList = null;
        try{
            stationsList = flowSearchDao.queryStationList();
        }catch (Exception e){
            log.error("queryStationList error:{}",e.getMessage(),e);
            Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getStations end");
        return Result.success(stationsList);
    }

    /**
     * 获取关键字总pv量 (关键词类型点击量统计)
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchKeyType>> getKeyByType(SearchStatisticsQuery queryParam) {
        log.info("getKeyByType start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchKeyType> keyByTypeList = null;
        try{
            keyByTypeList = flowSearchDao.queryKeyByType(queryParam);
        }catch(Exception e){
            log.error("queryKeyBytype error: {}",e.getMessage(),e);
            return  Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getKeyByType end");
        return Result.success(keyByTypeList);
    }

    /**
     * 获取关键字的日pv量(关键词类型点击量趋式)
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchKeyType>> getKeyByDate(SearchStatisticsQuery queryParam) {
        log.info("getKeyByDate start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchKeyType> keyByDateList = null;
        try{
            keyByDateList = flowSearchDao.queryKeyByDate(queryParam);
        }catch (Exception e){
            log.error("queryKeyByDate error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getKeyByDate end");
        return Result.success(keyByDateList);
    }

    /**
     * 获取关键字点击 各类型在时间段内 某站点 搜索次数  的景点信息(关键词详情列表)
     * @param queryParam
     * @return
     */
    @Override
    public Result<PageInfo<FlowSearchKey>> getKeyByAmount(SearchStatisticsQuery queryParam) {
        log.info("getKeyByAmount start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        PageHelper.startPage(queryParam.getPageCurrentNum(),queryParam.getPageSize());
        List<FlowSearchKey> keyByDateList = null;
        try{
            keyByDateList =flowSearchDao.queryKeyByAmount(queryParam);
        }catch (Exception e){
            log.error("queryKeyByAmount error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getKeyByAmount end");
        return Result.success(new PageInfo<FlowSearchKey>(keyByDateList));
    }

    /**
     * 关键字详情（用户所在地区前十）
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchKey>> getDetailByStation(SearchStatisticsQuery queryParam) {
        log.info("getDetailByStation start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchKey> detailByStationList = null;
        try{
            detailByStationList =flowSearchDao.queryDetailByStation(queryParam);
        }catch (Exception e){
            log.error("queryDetailByStation error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getDetailByStation end");
        return Result.success(detailByStationList);
    }

    /**
     * 关键字类型分布
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchKey>> getDetailByType(SearchStatisticsQuery queryParam) {
        log.info("getDetailByType start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchKey> detailByStationList = null;
        try{
            detailByStationList =flowSearchDao.queryDetailByKey(queryParam);
        }catch (Exception e){
            log.error("queryDetailByType error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getDetailByType end");
        return Result.success(detailByStationList);
    }

    /**
     * 关键词搜索到达品类分析
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchArrivalKey>> getKeyByCategory(SearchStatisticsQuery queryParam) {
        log.info("getKeyByCategory start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchArrivalKey> KeyByCategoryList = null;
        try{
            KeyByCategoryList =flowSearchDao.queryKeyByCategory(queryParam);
        }catch (Exception e){
            log.error("queryKeyByCategory error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getKeyByCategory end");
        return Result.success(KeyByCategoryList);
    }

    /**
     * 关键字到达品类分析 搜索词
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchArrivalKey>> getKeyByKeyword(SearchStatisticsQuery queryParam) {
        log.info("getKeyByKeyword start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchArrivalKey> KeyByKeywordList = null;
        try{
            KeyByKeywordList =flowSearchDao.queryKeyByKeyword(queryParam);
        }catch (Exception e){
            log.error("queryKeyByKeyword error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getKeyByKeyword end");
        return Result.success(KeyByKeywordList);
    }

    /**
     * 关键字到达品类分析 关键词
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchArrivalKey>> getKeyByXgword(SearchStatisticsQuery queryParam) {
        log.info("getKeyByXgword start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchArrivalKey> KeyByKeywordList = null;
        try{
            KeyByKeywordList =flowSearchDao.queryKeyByXgword(queryParam);
        }catch (Exception e){
            log.error("queryKeyByXgword error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getKeyByXgword end");
        return Result.success(KeyByKeywordList);
    }

    /**
     * 跳转分析 切换标签 切换跳转品类统计
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchJump>> getSwitchByCategory(SearchStatisticsQuery queryParam) {
        log.info("getSwitchByCategory start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchJump> switchByCategoryList = null;
        try{
            switchByCategoryList =flowSearchDao.querySwitchByCategory(queryParam);
        }catch (Exception e){
            log.error("querySwitchByCategory error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getSwitchByCategory end");
        return Result.success(switchByCategoryList);
    }

    /**
     * 跳转分析 切换标签 切换跳转品类趋式
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchJump>> getSwitchByDate(SearchStatisticsQuery queryParam) {
        log.info("getSwitchByDate start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchJump> switchByDateList = null;
        try{
            switchByDateList =flowSearchDao.querySwitchByDate(queryParam);
        }catch (Exception e){
            log.error("querySwitchByDate error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getSwitchByDate end");
        return Result.success(switchByDateList);
    }

    /**
     * 跳转分析 默认标签 跳转默认标签品类统计
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchJump>> getJumpByCategory(SearchStatisticsQuery queryParam) {
        log.info("getJumpByCategory start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchJump> jumpByCategoryList = null;
        try{
            jumpByCategoryList =flowSearchDao.queryJumpByCategory(queryParam);
        }catch (Exception e){
            log.error("queryJumpByCategory error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getJumpByCategory end");
        return Result.success(jumpByCategoryList);
    }

    /**
     * 跳转分析 默认标签 跳转默认品类趋式
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchJump>> getJumpByDate(SearchStatisticsQuery queryParam) {
        log.info("getJumpByDate start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchJump> jumpByDateList = null;
        try{
            jumpByDateList =flowSearchDao.queryJumpByDate(queryParam);
        }catch (Exception e){
            log.error("queryJumpByDate error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getJumpByDate end");
        return Result.success(jumpByDateList);
    }

    /**
     * 跳转分析 切换标签 图表详情
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchJump>> getSwitchByResult(SearchStatisticsQuery queryParam) {
        log.info("getSwitchByResult start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchJump> switchByResultList = null;
        try{
            switchByResultList =flowSearchDao.querySwitchByResult(queryParam);
        }catch (Exception e){
            log.error("querySwitchByResult error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getSwitchByResult end");
        return Result.success(switchByResultList);
    }

    /**
     * 跳转分析 默认标签 图表详情
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchJump>> getMoJumpByResult(SearchStatisticsQuery queryParam) {
        log.info("getMoJumpByResult start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchJump> moJumpByResultList = null;
        try{
            moJumpByResultList =flowSearchDao.queryJumpByResult(queryParam);
        }catch (Exception e){
            log.error("queryMoJumpByResult error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getMoJumpByResult end");
        return Result.success(moJumpByResultList);
    }

    /**
     * 搜索结果分析 品类搜索无结果统计
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchResult>> getResultByCategory(SearchStatisticsQuery queryParam) {
        log.info("getResultByCategory start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchResult> resultByCategoryList = null;
        try{
            resultByCategoryList =flowSearchDao.queryResultByCategory(queryParam);
        }catch (Exception e){
            log.error("queryResultByCategory error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getResultByCategory end");
        return Result.success(resultByCategoryList);
    }
    /**
     * 搜索结果分析 品类搜索无结果趋势
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchResult>> getResultByDate(SearchStatisticsQuery queryParam) {
        log.info("getResultByDate start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchResult> resultByDateList = null;
        try{
            resultByDateList =flowSearchDao.queryResultByDate(queryParam);
        }catch (Exception e){
            log.error("queryResultByDate error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getResultByDate end");
        return Result.success(resultByDateList);
    }

    /**
     * 搜索结果分析  详情列表
     * @param queryParam
     * @return
     */
    @Override
    public Result<PageInfo<FlowSearchResult>> getResultByKey(SearchStatisticsQuery queryParam) {
        log.info("getResultByKey start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        PageHelper.startPage(queryParam.getPageCurrentNum(),queryParam.getPageSize());
        PageInfo<FlowSearchResult> resultByKeyList = null;
        try{
            resultByKeyList =new PageInfo<FlowSearchResult>(flowSearchDao.queryResultByKey(queryParam));
        }catch (Exception e){
            log.error("queryResultByKey error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getResultByKey end");
        return Result.success(resultByKeyList);
    }

    /**
     * 筛选条件分析
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchFilter>> getFilterList(SearchStatisticsQuery queryParam) {
        log.info("getFilterList start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchFilter> FilterList = null;
        try{
            FilterList =flowSearchDao.queryFilterByCondition(queryParam);
        }catch (Exception e){
            log.error("queryFilterByCondition error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getFilterList end");
        return Result.success(FilterList);
    }

    /**
     * 查询筛选条件code名称
     * @param queryParam
     * @return
     */
    @Override
    public Result<FlowSearchFilterCondition> getFilterNameByCode(FlowSearchFilterCondition queryParam) {
        return Result.success(flowSearchDao.queryFilterNameByCode(queryParam));
    }

    /**
     * 筛选条件插入表中
     * @param queryParam
     * @return
     */
    @Override
    public boolean insertFilter(FlowSearchFilterCondition queryParam) {
        return flowSearchDao.insertFilter(queryParam);
    }

    /**
     * 翻页分析 查询各品类翻页次数
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchPaging>> getPagingByCategory(SearchStatisticsQuery queryParam) {
        log.info("getPagingByCategory start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchPaging> KeyByKeywordList = null;
        try{
            KeyByKeywordList =flowSearchDao.queryPagingByCategory(queryParam);
        }catch (Exception e){
            log.error("queryPagingByCategory error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getPagingByCategory end");
        return Result.success(KeyByKeywordList);
    }

    /**
     * 翻页分析，查询翻页数
     * @param queryParam
     * @return
     */
    @Override
    public Result<List<FlowSearchPaging>> getPagingByPageCode(SearchStatisticsQuery queryParam) {
        log.info("getPagingByPageCode start queryParam:{}",ToStringBuilder.reflectionToString(queryParam, ToStringStyle.DEFAULT_STYLE));
        List<FlowSearchPaging> KeyByKeywordList = null;
        try{
            KeyByKeywordList =flowSearchDao.queryPagingByPageCode(queryParam);
        }catch (Exception e){
            log.error("queryPagingByPageCode error: {}",e.getMessage(),e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("getPagingByPageCode end");
        return Result.success(KeyByKeywordList);
    }
}
